Matlab使用switch来判断执行哪条语句

您所在的位置:网站首页 matlab里面switch fi Matlab使用switch来判断执行哪条语句

Matlab使用switch来判断执行哪条语句

2023-04-01 04:32| 来源: 网络整理| 查看: 265

文章目录

展开 常见用法语法说明比较单个值与多个值进行比较友情提示 3.5 (4)

前文中,我们说了Matlab使用for编写循环语句,使用if编写判断语句,使用while编写循环执行语句。今天,我们再带来一篇使用switch来判断多条语句中判断需要执行的语句。你可以理解为给定一个值,判断该值的大小,根据不同的值,输出不同的语句。当然,跟通俗的理解为:多个值的判断比较。

本文,我们主要讲解Matlab中switch函数的常见用法、语法说明、比较单个值、与多个值进行比较。当然,本文对Matlab编程与数据处理方面,都有所帮助。

Matlab使用switch来判断执行哪条语句

下面,我们首先给出Matlab中关于switch函数的帮助文档如下:

>> help switch switch Switch among several cases based on expression. The general form of the switch statement is: switch switch_expr CASE case_expr, statement, ..., statement CASE {case_expr1, case_expr2, case_expr3,...} statement, ..., statement ... OTHERWISE, statement, ..., statement END The statements following the first CASE where the switch_expr matches the case_expr are executed. When the case expression is a cell array (as in the second case above), the case_expr matches if any of the elements of the cell array match the switch expression. If none of the case expressions match the switch expression then the OTHERWISE case is executed (if it exists). Only one CASE is executed and execution resumes with the statement after the END. The switch_expr can be a scalar or a string. A scalar switch_expr matches a case_expr if switch_expr==case_expr. A string switch_expr matches a case_expr if strcmp(switch_expr,case_expr) returns 1 (true). Only the statements between the matching CASE and the next CASE, OTHERWISE, or END are executed. Unlike C, the switch statement does not fall through (so BREAKs are unnecessary). Example: To execute a certain block of code based on what the string, METHOD, is set to, method = 'Bilinear'; switch lower(method) case {'linear','bilinear'} disp('Method is linear') case 'cubic' disp('Method is cubic') case 'nearest' disp('Method is nearest') otherwise disp('Unknown method.') end Method is linear常见用法switch switch_expression case case_expression statements case case_expression statements ... otherwise statements end语法说明

switch switch_expression, case case_expression, end计算表达式并选择执行多组语句中的一组。每个选项为一个case。

switch块会测试每个case,直至一个case表达式为true。case在以下情况下为true:

对于数字,case_expression == switch_expression。对于字符向量,strcmp(case_expression,switch_expression) == 1。对于支持 eq 函数的对象,case_expression == switch_expression。重载的 eq 函数的输出必须为逻辑值或可转换为逻辑值。对于元胞数组 case_expression,元胞数组的至少一个元素与 switch_expression 匹配,如上述对数字、字符向量和对象的定义。

当case表达式为true时,MATLAB® 执行对应的语句,然后退出switch块。

计算的switch_expression必须为标量或字符向量。计算的case_expression必须为标量、字符向量或者标量或字符向量的元胞数组。

otherwise块是可选的。仅当没有case为true时,MATLAB才会执行这些语句。

比较单个值

根据在命令提示符下输入的值有条件地显示不同的文本。

n = input('Enter a number: '); switch n case -1 disp('negative one') case 0 disp('zero') case 1 disp('positive one') otherwise disp('other value') end

在命令提示符下,输入数字1。输出结果如下:

positive one

重复执行该代码并输入数字3。输出结果如下:

other value与多个值进行比较

基于plottype的值确定要创建哪种类型的绘图。如果plottype为pie或pie3,则创建一个三维饼图。使用元胞数组包含两个值。

x = [12 64 24]; plottype = 'pie3'; switch plottype case 'bar' bar(x) title('Bar Graph') case {'pie','pie3'} pie3(x) title('Pie Chart') otherwise warning('Unexpected plot type. No plot created.') endMatlab使用switch来判断执行哪条语句友情提示

case_expression不能包含关系运算符(例如 < 或 >)来与switch_expression进行比较。要测试不相等性,请使用if, elseif, else语句。

MATLAB switch语句不会像C语言的switch语句一样失效。如果第一 case语句为true,则MATLAB不会执行其他case语句。例如:

result = 52; switch(result) case 52 disp('result is 52') case {52, 78} disp('result is 52 or 78') end result is 52

在该case内定义特定case中的代码所需要的变量。由于MATLAB仅执行任何switch语句的一个case,因此一个case内定义的变量不适用于其他case。例如,如果当前工作区不包含变量x,则仅定义x的情况可以使用它:

switch choice case 1 x = -pi:0.01:pi; case 2 % does not know anything about x end

MATLAB break语句会结束for或while循环的执行,但不结束switch语句的执行。此行为不同于C语言中break和switch的行为。

共计4人评分,平均3.5分

到目前为止还没有投票~

很抱歉,这篇文章对您没有用!

让我们改善这篇文章!

告诉我们我们如何改善这篇文章?

转载文章,原文出处:MathWorks官网,由古哥整理发布

如若转载,请注明出处:https://iymark.com/articles/2329.html



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3